// pages/softStep1/softStep1.js
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    project_id:'',
    line1: '',
    line1Error: false,
    line1MinNum: 2,
    line1MaxNum: 20,

    line2: '',
    line2Error: false,
    line2MinNum: 45,
    line2MaxNum: 120,

    line3: '',
    line3Error: false,
    line3MinNum: 45,
    line3MaxNum: 120,

    imageFileId : -1,
    imageBasecode: '',
    imageUploadProgress: 0,
    loadingOriginalImage: false,

    picUrls: [],
    files: []
  },

  chooseImage: function(e) {
    
    this.setData({
      files: []
    })
    if (this.data.files.length >= 1) {
      util.showErrorToast('只能上传1张图片')
      return false;
    }

    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        that.data.imageUploadProgress = 0
        that.setData({
          files: [res.tempFilePaths]
        });
        that.upload(res);
      }
    })
  },
  upload: function(res) {
    var url = 'https://register-pub.com/image_upload.php?sign='+this.data.project_id
    var that = this;
    const uploadTask = wx.uploadFile({
      url: url,
      filePath: res.tempFilePaths[0],
      name: 'file',
      success: function(res) {
        var _res = JSON.parse(res.data);
        if (_res.code === 0) {
          that.setData({
            imageFileId: _res.data.id,
            imageBasecode: _res.data.basecode
          })
        }
      },
      fail: function(e) {
        wx.showModal({
          title: '错误',
          content: '上传失败',
          showCancel: false
        })
      },
    })

    uploadTask.onProgressUpdate((res) => {
      console.log('上传进度', res.progress)
      console.log('已经上传的数据长度', res.totalBytesSent)
      console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
      that.setData({
        imageUploadProgress: res.progress
      })
    })

  },
  previewImage: function(e) {
    return
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },

  bindInput(e){
    this.setData({
      line1: e.detail.value
    });
    console.log(e.detail.value)
  },

  bindInput2(e){
    this.setData({
      line2: e.detail.value
    });
    console.log(e.detail.value)
  },

  bindInput3(e){
    this.setData({
      line3: e.detail.value
    });
    console.log(e.detail.value)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.title){
      this.setData({
        title: options.title
      })
      
    }
    if (options.project_id){
      this.setData({
        project_id: options.project_id
      }) 
    }
    console.log(this.data.project_id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    var pid = this.data.project_id
    var docItemKey = 'pid_'+pid
    var stableDocData = wx.getStorageSync(docItemKey)
    var that = this
    if(stableDocData){
        var docJson = JSON.parse(stableDocData)
        if(docJson.step10Data!==undefined && docJson.step10Data!=null){
            if(docJson.step10Data.line1!==undefined && docJson.step10Data.line1!=null){
              this.setData({
                line1: docJson.step10Data.line1
              }) 
            }
            if(docJson.step10Data.line2!==undefined && docJson.step10Data.line2!=null){
              this.setData({
                line2: docJson.step10Data.line2
              }) 
            }
            if(docJson.step10Data.line3!==undefined && docJson.step10Data.line3!=null){
              this.setData({
                line3: docJson.step10Data.line3
              }) 
            }
            if(docJson.step10Data.imageFileId!==undefined && docJson.step10Data.imageFileId!=null && docJson.step10Data.imageFileId!==-1){
                that.setData({
                  loadingOriginalImage: true
                })
                var getImageUrl = 'https://register-pub.com/image_check.php?id='+docJson.step10Data.imageFileId+'&sign='+this.data.project_id
                wx.request({
                  url: getImageUrl,
                  method: 'GET', // 可选，默认为 GET
                  header: {
                    'content-type': 'application/json' // 默认值
                  },
                  success: res => {
                    console.log(res.data)
                    var _res = res.data
                    console.log(_res.data.id)
                    that.setData({
                      loadingOriginalImage: false,
                      imageFileId : _res.data.id
                    })
                    if(_res.code===0 && that.data.imageFileId!=-1){
                        //layui.$('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', _res.data.basecode);
                        that.setData({
                          imageFileId: _res.data.id,
                          imageBasecode: _res.data.basecode,
                          files: [_res.data.basecode]
                        })
                        //document.getElementById("upload_image_tip").textContent = '点击此处重新上传，或将文件拖拽到此处'

                    }else {
                      wx.showModal({
                        title: '错误',
                        content: '图片已经失效，请重传',
                        showCancel: false
                      })
                    }

                  },
                  fail: err => {
                    that.setData({
                      loadingOriginalImage: false
                    })
                    wx.showModal({
                      title: '错误',
                      content: '网络错误',
                      showCancel: false
                    })
                  }
                })

                
            }
        }
    }
  },

  onSave(event){
    console.log(this.data.line1)
    if(!util.checkInputNum('line1',this.data.line1,'步骤1标题',this.data.line1MinNum,this.data.line1MaxNum)){
      this.setData({
        line1Error: true
      })
      return;
    }

    this.setData({
      line1Error: false
    })

    if(!util.checkInputNum('line2',this.data.line2,'步骤3第一段概述',this.data.line2MinNum,this.data.line2MaxNum)){
      this.setData({
        line2Error: true
      })
      return;
    }

    this.setData({
      line2Error: false
    })

    if(!util.checkInputNum('line3',this.data.line3,'步骤3第二段概述',this.data.line3MinNum,this.data.line3MaxNum)){
      this.setData({
        line3Error: true
      })
      return;
    }

    this.setData({
      line3Error: false
    })

    if(this.data.imageFileId===-1){
      wx.showModal({
        title: '错误信息',
        content: '请上传图片',
        showCancel: false
      });
      return;
    }

    var nowData = {}
    nowData.line1 = this.data.line1
    nowData.line2 = this.data.line2
    nowData.line3 = this.data.line3
    nowData.imageFileId = this.data.imageFileId

    var pid = this.data.project_id
    var itemKey = 'pid_'+pid
    var value = wx.getStorageSync(itemKey)
    var docJson = JSON.parse(value)
    docJson.step10Data = nowData
    wx.setStorageSync(itemKey,JSON.stringify(docJson))
    wx.navigateBack()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})